package com.supor.aiot.mine.ui.feedback;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseConfigActivity;
import com.supor.aiot.common.base.NoViewModel;
import com.supor.aiot.common.utils.BitmapUtil;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.databinding.ActivityPhotoBinding;
import com.supor.aiot.mine.helper.FeedbackImageCache;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: 反馈图片展示 <br>
 * 作者: lei <br>
 * 日期: 2021/1/31 <br>
 */
public class PhotoActivity extends BaseConfigActivity<NoViewModel, ActivityPhotoBinding> {

    private List<String> drr = new ArrayList<String>();
    private List<View> listViews;

    @Override
    protected int resourceId() {
        return R.layout.activity_photo;
    }

    @Override
    protected void initData() {
        super.initData();
        drr.addAll(FeedbackImageCache.getInstance().getDrr());

        ViewPager pager;
        (pager = (ViewPager) findViewById(R.id.viewpager)).addOnPageChangeListener(pageChangeListener);
        for (int k = 0; k < drr.size(); ++k) {
            initListViews(drr.get(k));
        }
        PhotoViewAdapter adapter = new PhotoViewAdapter(listViews);
        pager.setAdapter(adapter);
        pager.setCurrentItem(getIntent().getIntExtra(Constants.Key.ID, 0));
    }

    private ViewPager.OnPageChangeListener pageChangeListener =
            new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrollStateChanged(final int state) {
                }

                @Override
                public void onPageScrolled(final int position, final float positionOffset,
                                           final int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(final int position) {
                }
            };

    private void initListViews(final String path) {
        if (listViews == null) {
            listViews = new ArrayList<>();
        }
        final ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        imageView.setOnClickListener(v -> finish());
        BitmapUtil.loadLocal(this, path, imageView, BitmapUtil.options3);
        listViews.add(imageView);
    }

    private static class PhotoViewAdapter extends PagerAdapter {
        private List<View> listViews;
        private int size;

        public PhotoViewAdapter(final List<View> listViews) {
            this.listViews = listViews;
            if (listViews == null) {
                size = 0;
            } else {
                size = listViews.size();
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(listViews.get(position % size));
        }

        @Override
        public int getCount() {
            return size;
        }

        @Override
        public int getItemPosition(final Object o) {
            return -2;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(listViews.get(position % size), 0);
            return listViews.get(position % size);
        }

        @Override
        public boolean isViewFromObject(final View view, final Object o) {
            return view == o;
        }
    }
}
